package me.zhengjie.util;

import com.alibaba.fastjson.JSON;
import me.zhengjie.config.RedisConfig;
import me.zhengjie.exception.ParameterException;
import me.zhengjie.util.ruoyiexcel.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 获取连接池对象
 */
public enum RedisUtils {
    INSTANCE;
    static JedisPool jedisPool = null;

    static {
        //1 创建连接池配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2 进行配置-四个配置
        config.setMaxIdle(1);//最小连接数
        config.setMaxTotal(500);//最大连接数
        config.setMaxWaitMillis(10 * 1000L);//最长等待时间
        config.setTestOnBorrow(true);//测试连接时是否畅通
//        String host = properties.getProperty("redis.host");
//        String port = properties.getProperty("redis.port");
//        String password = properties.getProperty("redis.password");
//        String timeout = properties.getProperty("redis.timeout");
        String host = RedisConfig.getHost();
        String port = RedisConfig.getPort();
        String password = RedisConfig.getPassword();
        String timeout = RedisConfig.getTimeout();
        System.err.println("yml:" + RedisConfig.getPort());
        System.err.println("yml:" + RedisConfig.getDatabase());
        System.err.println("yml:" + RedisConfig.getHost());
        System.err.println("yml:" + RedisConfig.getTimeout());
        System.err.println("yml:" + RedisConfig.getPassword());
        if (StringUtils.isBlank(password)) {
            jedisPool = new JedisPool(config, host, Integer.valueOf(port), Integer.valueOf(timeout));
        } else {
            jedisPool = new JedisPool(config, host, Integer.valueOf(port), Integer.valueOf(timeout), password);
        }
    }

    //获取连接
    public Jedis getSource() {
        return jedisPool.getResource();
    }

    //关闭资源
    public void closeSource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }

    }

    /**
     * 设置字符值
     *
     * @param key
     * @param
     */
    public void del(String key) {
        Jedis jedis = getSource();
        jedis.del(key);
        closeSource(jedis);
    }

    /**
     * 设置字符值
     *
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        Jedis jedis = getSource();
        jedis.set(key, value);
        closeSource(jedis);
    }

    /**
     * 设置
     *
     * @param key
     * @param value
     */
    public void set(byte[] key, byte[] value) {
        Jedis jedis = getSource();
        jedis.set(key, value);
        closeSource(jedis);
    }

    /**
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        Jedis jedis = getSource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSource(jedis);
        }
        return null;

    }

    /**
     * 获取字符值
     *
     * @param key
     */
    public String get(String key) {
        Jedis jedis = getSource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSource(jedis);
        }

        return null;

    }

    public void set(String key, String value, Integer time) {
        Jedis jedis = getSource();
        jedis.setex(key, time, value);
        closeSource(jedis);
    }

    /**
     * 通过userToken获取当前登录对象
     *
     * @param userToken 当前登录对象的userToken
     * @param clazz     当前登陆对象对象的字节码
     * @param <T>       泛型
     * @return 当前登录对象
     */
    public <T> T getLoginInfo(String userToken, Class<T> clazz) {
        Jedis jedis = getSource();
        String loginJson = jedis.get(userToken);
//        String loginJson = INSTANCE.get(userToken);
        closeSource(jedis);
        if (StringUtils.isBlank(loginJson)) {
            throw new ParameterException("登录信息为空");
        }

        return JSON.parseObject(loginJson, clazz);
    }
}